/*
 *	base class for positional objects
 *	uses quaternion internally for orientation
 *	by GMKal
 */

#ifndef POSOBJECT_H
#define POSOBJECT_H


#include "glmath.h"


using namespace glmath;






class PositionalObject
{
protected:
	/** the object's position in 3d space */
	Vector3	mPosition;				
	/** object's orientation */
	Quaternion mRotation;	


public:
	PositionalObject() {};
	PositionalObject(const Vector3& vec,const Quaternion& quat)
		: mPosition(vec),mRotation(quat) {};
	PositionalObject(GLfloat &xpos,GLfloat &ypos,GLfloat &zpos,
		GLfloat xangle=0.0f,GLfloat yangle=0.0f,GLfloat zangle=0.0f)
		:	mPosition(Vector3(xpos,ypos,zpos))
	{	mRotation.SetFromRadians(xangle,yangle,zangle);	}




	void Draw();



	const Quaternion& GetRotation() const {	return mRotation;	}
	const Vector3& GetPosition() const {	return mPosition;	}
	void SetPosition(const Vector3& vec) {	mPosition = vec;	}
	void SetRotation(GLdouble xrad,GLdouble yrad,GLdouble zrad) {	mRotation.SetFromRadians(xrad,yrad,zrad);	}
	void SetRotation(GLdouble angle,GLfloat x,GLfloat y,GLfloat z) {	mRotation.SetFromAxis(angle,x,y,z);	}
	void SetRotation(const Quaternion& quat) {	mRotation = quat;	}

	/** add vdelta to current position */
	void MoveBy(const Vector3& vdelta) { mPosition += vdelta;	}

	void RotateBy(const Quaternion& quat) {	mRotation *= quat;	}
	void RotateBy(double xrad,double yrad,double zrad) {	mRotation *= Quaternion().SetFromRadians(xrad,yrad,zrad);	}
	void RotateBy(double rads,GLfloat x,GLfloat y,GLfloat z) {	mRotation *= Quaternion().SetFromAxis(rads,x,y,z);		}

	/** where is the object looking? */
	inline Vector3 GetDirVec();

	inline void AlignTo(const PositionalObject& obj);

	/** change object's orientation so it looks at target */
	void PointAt(const Vector3& target);
	
};

inline PositionalObject::GetDirVec()
{	
	mRotation.Normalize();
	return mRotation.GetDirectionVector();	
}

inline PositionalObject::AlignTo(const PositionalObject& obj)
{
	SetPosition(obj.GetPosition());
	SetRotation(obj.GetRotation());
}


#endif